All Questions
14 questions
7votes
2answers
6kviews
Permutations with replacement in Python
Many a times, I've had the need to use a permutations with replacement function. So, I've written a function to do just that: ...
3votes
2answers
272views
Fixed Content Necklace Generator
I wrote a class that generates combinatorial necklaces with fixed content as per Sawada: A fast algorithm to generate necklaces with fixed content. The class is instantiated with an input list that ...
4votes
3answers
540views
Generate parentheses solution
I have coded a solution to build all valid permutations of parentheses. My code is below. I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to ...
4votes
1answer
4kviews
Recursive implementation of power set in Python
I was doing a recursion problem called power set, and would like to get code review. Here's the problem. ...
3votes
1answer
254views
python magic square finder for arbitrary numbers
I wrote this code to get all possible magic squares that can be made with a list you put in. The only contraints are that the count of numbers is a square and that each number is unique. It even works ...
2votes
1answer
9kviews
Computing the powerset of a list
I've written the following code for computing the powerset of a list (we consider the list has all distinct elements, although it doesn't really matter). Can this function be further optimized? <...
4votes
1answer
3kviews
K-combination recursive algorithm implementation
Tried to implement this solution for finding n choose k. I have used recursion and this is how it goes for k as 4 and n as 6. Zero based The idea is to have array of size k keeping sequence of ...
6votes
2answers
7kviews
Making the same amount from different combinations of coins (top-down approach)
I was reading a typical interview question found here. Given an amount and a list of denominations, count the number of possible ...
4votes
1answer
408views
Counting ways to fit bottles in crates
I'm calculating how many possibilities there are to fit \$n\$ identical bottles inside \$k\$ crates of various capacities. n = Number of bottles k = Number of crates K = List of the number of bottles ...
8votes
1answer
983views
Recursive search for combinations of words that have a specified MD5 hash
This code solves a challenge problem that a local company is hosting to attract/screen applicants. The link to the challenge is here. In brief, we are given a wordlist containing approximately 100,...
4votes
4answers
9kviews
Staircase problem solved using recursion
Problem Question taken from here. I want to go up a flight of stairs that has n steps. I can either take 1 or 2 steps each time. How many different ways can I go up this flight of stairs? Write a ...
2votes
1answer
3kviews
Insect combinatorics challenge
Below is the problem taken from Berkeley's Cs61A page here Question 9: Insect Combinatorics* Consider an insect in an M by N grid. The insect starts at the bottom left corner, (0, 0), and wants ...
2votes
2answers
1kviews
Count the number of combinations with gap-restriction for every pair of values
A lottery draw consists of choosing 6 different values from 1 to 50. I want to calculate the number of combinations with gaps of at least 3 between every pair of values. I have used the following ...
2votes
1answer
6kviews
Nsum problem: find all n elements in an array whose sum equals a given value
I am pretty new to Python, so I want to listen to any advice that improve my coding style in a "pythonic" way, even about naming style of variables. The following code reflects a very general paradigm ...